//index.js
//获取应用实例
var app = getApp()
var a = null;
Page({
  data: {
    goodsID:'',
    imgUrls: '',
    title: '',
    price1: '',
    imgsrv: app.globalData.srvlink + '/images/',
    goodsList: [],
    status: 0,
    currentPosition: "order0",
    imgUrls: [
      "/images/1.jpg",
      "/images/2.jpg",
      "/images/3.jpg"
    ],

  },

  changeMenu: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      status: index,
      currentPosition: "order" + index
    })
  },
  getGoodsList() {
    var that = this
    wx.showLoading();
    wx.request({
      url: app.globalData.srvlink + 'goodsList',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()

        var tmp = res.data.data;
        var type=''
        var idx=0;
        for(var i=0;i<tmp.length;i++){
           
          if(type!=tmp[i].goodsType){
            tmp[i].idx = idx;
            idx++;
            type = tmp[i].goodsType;
          }else{
            tmp[i].goodsTypeName = '';
          }
        }
        //console.log(tmp);
        that.setData({ goodsList: tmp })

      }
    })
  },

  onLoad: function (e) {
    console.log(e);
    a = e;
    this.setData({
      title: e.title,
      imgUrls: e.imgUrls,
      price1: e.price1,

    });

    this.getGoodsList();
  },
  scrollBottom: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    setTimeout(function () {
      wx.hideLoading();
    }, 1000)
  },

  

  join: function (e) {
    var icon = 0;
    var id=e.currentTarget.dataset.goodsid
    var carts = app.globalData.joinCar;
    for (let i = 0; i < carts.length; i++) {
      if(carts[i].goodsId==id){
        icon=1;
        break;
      }
    }

   var target=null;
   for(var i=0;i<this.data.goodsList.length;i++){
     if (this.data.goodsList[i].goodsId== id){
       target = this.data.goodsList[i];
       break;
     }
   }
     
    if (icon == 0 && target!=null) {
      app.globalData.joinCar.push(target);
      console.log("添加餐饮成功")
      this.joinT();
    }
  },
  joinT: function () {
    wx.showModal({
      title: '恭喜',
      content: '成功添加餐饮至购物车',
    })
  },
})
